<template>
    <div id="app">
        <!--搜索框-->
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <!--作者框-->
            <el-form-item label="作者">
                <el-input v-model="formInline.author" placeholder="作者"></el-input>
            </el-form-item>
            <!--申报类型-->
            <el-form-item label="新闻题目">
                <el-input v-model="formInline.title" placeholder="新闻题目"></el-input>
            </el-form-item>
            <!--时间选择框-->
            <el-form-item label="申请时间">
                <el-date-picker
                        v-model="formInline.time"
                        type="daterange"
                        value-format="yyyy-MM-dd"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期">
                </el-date-picker>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="initTable()">查询</el-button>
            </el-form-item>
        </el-form>

        <el-table
                :data="tableData"
                style="width: 100%"
                :row-class-name="tableRowClassName">
            <el-table-column
                    prop="id"
                    label="编号">
            </el-table-column>
            <el-table-column
                    prop="title"
                    width="300"
                    label="新闻题目">
            </el-table-column>
            <el-table-column
                    prop="kind"
                    label="新闻类型">
            </el-table-column>
            <el-table-column
                    prop="author"
                    label="作者">
            </el-table-column>
            <el-table-column
                    prop="time"
                    label="申请时间">
            </el-table-column>
            <el-table-column
                    prop="user"
                    label="审核人">
            </el-table-column>
            <el-table-column
                    prop="outtime"
                    label="审核时间">
            </el-table-column>
            <el-table-column
                    label="状态">
                <template slot-scope="scope">
                    <span v-if="scope.row.status==9">待审核</span>
                    <span v-if="scope.row.status==1">审核通过</span>
                    <span v-if="scope.row.status==2">新闻上架</span>
                    <span v-if="scope.row.status==3">审核不通过</span>
                    <span v-if="scope.row.status==4">新闻下架</span>
                </template>
            </el-table-column>
            <el-table-column
                    fixed="right"
                    label="操作"
                    width="290">
                <template slot-scope="scope">
                    <el-button @click="details(scope.row)" type="primary">详情</el-button>
                    <el-button v-if="scope.row.status==4" @click="upUpdate(scope.row)" type="success">上架</el-button>
                    <el-button v-if="scope.row.status==2" @click="upUpdate(scope.row)" type="danger">下架</el-button>
                    <el-button v-if="scope.row.status==1" @click="upUpdate(scope.row)" type="success">上架</el-button>
                    <el-button v-if="scope.row.status==9" @click="upUpdate(scope.row)" type="success" disabled>上架</el-button>
                    <el-button v-if="scope.row.status==3" @click="upUpdate(scope.row)" type="success" disabled>上架</el-button>
                    <el-button v-if="scope.row.uppid==6" @click="topUpdate(scope.row)" type="success">作品置顶</el-button>
                    <el-button v-if="scope.row.uppid==5" @click="topUpdate(scope.row)" type="danger">取消置顶</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--分页-->
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
        </el-pagination>

        <!--添加-->
        <el-dialog
                title="提示"
                :visible.sync="dialogVisible"
                width="30%"
                @close="dialogClose">
            <!--:model绑定表单数据-->
            <el-descriptions :model="editForm" title="详细信息" column="1">
                <el-descriptions-item label="新闻题目">{{editForm.title}}</el-descriptions-item>
                <el-descriptions-item label="作者">{{editForm.author}}</el-descriptions-item>
                <el-descriptions-item label="新闻内容">
                    <span>{{editForm.content}}</span>
                </el-descriptions-item>
            </el-descriptions>

            <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
          </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "newsUpDown",
        data(){
            return{
                tableData: [],
                editForm: {},
                dialogVisible: false,

                currentPage:1,
                total:0,
                pageSize:5,
                formInline:{},
            }
        },
        created(){
            this.initTable();
        },
        methods:{
            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 0) {
                    return 'warning-row';
                } else if (rowIndex === 2) {
                    return 'success-row';
                } else if (rowIndex === 4) {
                    return 'warning-row';
                }
                return '';
            },
            onSubmit(){
                this.currentPage=1;
                this.initTable();
            },
            handleSizeChange(val) {
                this.pageSize = val;
                this.initTable();
            },
            handleCurrentChange(val) {
                this.currentPage = val;
                this.initTable();
            },
            //初始化
            initTable(){
                this.$http.post("/system/news/list?currentPage="+this.currentPage+"&pageSize="+this.pageSize,this.formInline).then(resp=>{
                    console.log(resp);
                    this.tableData = resp.data.data.list;
                    this.total = resp.data.data.total;

                });
            },
            // 详细按钮
            details(e){
                this.dialogVisible=true;
                this.editForm=e;

            },
            // 上下架按钮
            upUpdate(e){
                this.$http.post("/system/news/upUpdate?id="+e.id+"&status="+e.status).then(resp=>{

                    this.initTable();
                });
            },
            //置顶和取消置顶按钮
            topUpdate(e){
                this.$http.post("/system/news/topUpdate?id="+e.id+"&uppid="+e.uppid).then(resp=>{
                    this.initTable();
                });
            },

        },
    }
</script>

<style scoped>
    .el-table .warning-row {
        background: oldlace;
    }

    .el-table .success-row {
        background: #f0f9eb;
    }
</style>